package com.youhong.teethcare.dialogs;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.youhong.teethcare.R;
import com.youhong.teethcare.utils.ScreenUtils;
import com.zyyoona7.wheel.WheelView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 *
 */
public class BrushModeDialog extends DialogFragment {


    Unbinder unbinder;
    List<String> WarnHighList;
    List<String> WarnLowList;
    public static final int SetWarn_BR = 166;
    public static final int SetWarn_HR = 676;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM_LowValue = "ARG_PARAM_LowValue";


    int SetWarnType = 0;
    public static final int Type_Time = 0;
    public static final int Type_Mode = 1;
    public static final int Type_Diy= 2;
    public static final int Type_Lv= 3;
    public static final int Type_Gender= 4;
    int lowValue = 0;
    int highValue = 0;
    @BindView(R.id.time_warning_title)
    TextView timeWarningTitle;
    @BindView(R.id.title_line)
    View titleLine;
    @BindView(R.id.tipview)
    View tipview;
    @BindView(R.id.time_cancel)
    Button timeCancel;
    @BindView(R.id.time_ok)
    Button timeOk;
    @BindView(R.id.submit_rt)
    RelativeLayout submitRt;
    @BindView(R.id.limit_low)
    WheelView limitLowWheelView;
//    @BindView(R.id.limit_high)
//    WheelView limitHighWheelView;
    Unbinder unbinder1;

    private int lowSelectedPosition;

    public static BrushModeDialog newInstance(int setWarnType, int lowValue) {

        BrushModeDialog fragment = new BrushModeDialog();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, setWarnType);
        args.putInt(ARG_PARAM_LowValue, lowValue);
        //args.putInt(ARG_PARAM_HighValue, highValue);
        fragment.setArguments(args);
        return fragment;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            SetWarnType = getArguments().getInt(ARG_PARAM1);
            lowSelectedPosition = getArguments().getInt(ARG_PARAM_LowValue);
           // highSelectedPosition = getArguments().getInt(ARG_PARAM_HighValue);
            //jstyleDevice= (JstyleDevice) getArguments().getSerializable(ARG_PARAM4);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity());
        View view = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_sleep_time, null);
        unbinder = ButterKnife.bind(this, view);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCancelable(false);
        dialog.setContentView(view);
        int height = ScreenUtils.dip2px(getContext(), 400);
        int width = ScreenUtils.getScreenWidth(getContext()) - ScreenUtils.dip2px(getContext(), 18);
        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
        layoutParams.width = width;
        layoutParams.height = height;
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.verticalMargin = 0.02f;
        layoutParams.windowAnimations = R.style.dialog_bottom_animstyle;
        dialog.getWindow().setAttributes(layoutParams);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        init();
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    public void setTitle(String title) {
        this.title = title;
    }

    public void setData(List dataHour) {
        this.WarnLowList = dataHour;

    }

    String title = "";

    private void init() {
        timeWarningTitle.setText(getTitle(SetWarnType));
      //  limitHighWheelView.setVisibility(SetWarnType==Type_Mode?View.VISIBLE:View.GONE);
      //  initWheelView(limitHighWheelView, WarnHighList, highSelectedPosition, "");
        initWheelView(limitLowWheelView, WarnLowList, lowSelectedPosition, "");

    }
    String getTitle(int type){
        String data="";
        if(type==Type_Time){
            data=getString(R.string.Brushing_time);
        }else if(type==Type_Mode){
            data=getString(R.string.brushingModeTitle);
        }else if(type==Type_Diy){
            data=getString(R.string.custom_mode_settings);
        }else if(type==Type_Gender){
            data=getString(R.string.strings50);
        }else{
            data=getString(R.string.lvTitle);
        }
        return  data;
    }

    private static final String TAG = "SleepWarnDialog";


    void initWheelView(WheelView wheelView, List data, int currentItem, String label) {
        wheelView.setData(data);
        wheelView.setDrawSelectedRect(true);
        wheelView.setVisibleItems(5);
        wheelView.setSelectedItemTextColor(getResources().getColor(R.color.mainColor));
        wheelView.setSelectedItemPosition(currentItem);
        wheelView.setLineSpacing(ScreenUtils.dip2px(getContext(), 30));

        wheelView.setSoundEffectResource(R.raw.snap_finger);
        wheelView.setDividerHeight(ScreenUtils.dip2px(getContext(), 0.5f));
        wheelView.setOnWheelChangedListener(new WheelView.OnWheelChangedListener() {
            @Override
            public void onWheelScroll(int scrollOffsetY) {
                
            }

            @Override
            public void onWheelItemChanged(int oldPosition, int newPosition) {
                Log.i(TAG, "onWheelItemChanged: "+newPosition);
            }

            @Override
            public void onWheelSelected(int position) {
                Log.i(TAG, "onWheelSelected: "+position);
            }

            @Override
            public void onWheelScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.time_cancel, R.id.time_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.time_cancel:
                dismiss();
                break;
            case R.id.time_ok:
                limitValue();
                dismiss();
                break;
        }
    }

    void limitValue() {
        if (limitValueListener == null) return;
        Log.i(TAG, "limitValue: ");
        int lowPosition = limitLowWheelView.getSelectedItemPosition();
       // int highPosition = limitHighWheelView.getSelectedItemPosition();
//        String lowLimitValue = WarnLowList.get(lowPosition);
//        String highLimitValue = WarnHighList.get(highPosition);
        limitValueListener.limitValueCallBack(SetWarnType, lowPosition);
    }

    LimitValueListener limitValueListener;

    public interface LimitValueListener {
        void limitValueCallBack(int type, int lowValue);
    }

    public void setLimitValueListener(LimitValueListener limitValueListener) {
        this.limitValueListener = limitValueListener;
    }

}
